#include "menu.h"


extern menu_t main_menu;


// 示例菜单项回调函数
void menu_func1(void) {
    lcd_clear(WHITE);
    lcd_show_string(10, 50, 200, 24, LCD_FONT_24, "music page", RED);
    // menu_draw(&main_menu);  // 重新绘制菜单
}

void menu_func2(void) {
    lcd_clear(WHITE);
    lcd_show_string(10, 50, 200, 24, LCD_FONT_24, "photo page", GRAY);
    // menu_draw(&main_menu);
}

void menu_func3(void) {
    lcd_clear(WHITE);
    lcd_show_string(10, 50, 200, 24, LCD_FONT_24, "file page", GREEN);
    // menu_draw(&main_menu);
}

void menu_func4(void) {
    lcd_clear(WHITE);
    lcd_show_string(10, 50, 200, 24, LCD_FONT_24, "text page", BLUE);
    // menu_draw(&main_menu);
}
void menu_func5(void) {
    lcd_clear(WHITE);
    lcd_show_string(10, 50, 200, 24, LCD_FONT_24, "about page", BLACK);
    // menu_draw(&main_menu);
}

// 菜单项定义
menu_item_t main_menu_items[] = {
    {"(1) play music", menu_func1},
    {"(2) take photo", menu_func2},
    {"(3) print file", menu_func3},
    {"(4) show text", menu_func4},
    {"(5) about", menu_func5}
};

// 初始化菜单
void menu_init(void) {
    main_menu.items = main_menu_items;
    main_menu.item_count = sizeof(main_menu_items) / sizeof(menu_item_t);
    main_menu.selected_index = 0;
    main_menu.x = 10;
    main_menu.y = 30;
    main_menu.width = 220;  // 菜单宽度
    main_menu.font = LCD_FONT_24;
    main_menu.normal_color = BLACK;
    main_menu.selected_color = RED;
}

// 获取字体高度
uint16_t get_font_height(lcd_font_t font) {
    switch (font) {
        case LCD_FONT_12: return 12;
        case LCD_FONT_16: return 16;
        case LCD_FONT_24: return 24;
        case LCD_FONT_32: return 32;
        default: return 24;
    }
}

// 绘制菜单
void menu_draw(menu_t *menu) {
    // lcd_clear(WHITE);  // 清屏
    uint16_t item_height = get_font_height(menu->font);
    
    // 绘制菜单标题
    lcd_show_string(menu->x, menu->y - 30, 200, 24, LCD_FONT_24, "ESP32 Menu Test", BLACK);
    
    // 绘制菜单项
    for (uint8_t i = 0; i < menu->item_count; i++) {
        uint16_t y_pos = menu->y + i * (item_height + 10);  // 项间距10像素
        
        // 选中项绘制下划线或反色
        if (i == menu->selected_index) {
            // 绘制选中标记
            lcd_show_string(menu->x - 20, y_pos, 20, item_height, 
                          menu->font, ">", menu->selected_color);
            // 绘制选中项文本
            lcd_show_string(menu->x, y_pos, menu->width, item_height,
                          menu->font, menu->items[i].title, menu->selected_color);
        } else {
            // 绘制普通项文本
            lcd_show_string(menu->x, y_pos, menu->width, item_height,
                          menu->font, menu->items[i].title, menu->normal_color);
        }
    }
}

// 处理按键输入
void handle_menu_key(uint8_t key) {
    Serial.printf("main_menu.selected_index = %d\n", main_menu.selected_index);
    Serial.printf("main_menu.item_count = %d\n", main_menu.item_count);
    Serial.printf("key = %d\n", key);
    Serial.printf("KEY_UP = %d\n", KEY_UP);
    Serial.printf("KEY_DOWN = %d\n", KEY_DOWN);
    switch (key) {
        case KEY_UP:
            if (main_menu.selected_index > 0) {
                Serial.println("KEY_UP press");
                main_menu.selected_index--;
                menu_draw(&main_menu);
            }
            break;
            
        case KEY_DOWN:
            if (main_menu.selected_index < main_menu.item_count - 1) {
                Serial.println("KEY_DOWN press");
                main_menu.selected_index++;
                menu_draw(&main_menu);
            }
            break;
            
        case KEY_ENTER:
            Serial.println("KEY_ENTER press");
            // 执行选中项的回调函数
            if (main_menu.items[main_menu.selected_index].callback) {
                main_menu.items[main_menu.selected_index].callback();
            }
            break;
            
        case KEY_BACK:
            Serial.println("KEY_BACK press");
            // 可用于返回上级菜单（如果有子菜单）
            lcd_clear(WHITE);  // 清屏
            menu_draw(&main_menu);
            break;
    }
}
